import { Context } from 'egg'

export default {
  // get userId() {
  //   if (this.state && this.state.user && this.state.userId) {
  //     return this.state.user.userId
  //   }
  //   // this.logger.info(this.state)
  //   throw new Error('no authorization user by JWT.')
  // },
  get userId(this: Context) {
    return this.state && this.state.user ? this.state.user.userId : null
  },
  get accessToken(this: Context) {
    const { authorization } = this.header
    const params = authorization.split(' ')
    if (params.length === 2) return params[1]
    return ''
  },
  get realUrl(this: Context) {
    return `${this.protocol}://${this.host}/`
  },
}
